package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: qingtao
 * @Date 2022/3/10 8:58
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    /**
     * 查所有
     * @return
     */
    public List<Category> findAll(){
        return categoryMapper.findAll();
    }

    /**
     * 根据类型查询
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findAllByType(Integer type) {
        return categoryMapper.findAllByType(type);
    }

    /**
     * 修改
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category) {
        category.setUpdateTime(LocalDateTime.now());

        //查询要修改的数据在数据库是否存在
        Category byName = categoryMapper.getByName(category);

        if (byName != null && !byName.getId().equals(category.getId())) {
            throw new BusinessException("修改后的名字已经存在,无法修改");
        }
        return categoryMapper.update(category) > 0;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        if (dishMapper.findDishCountByCategoryId(id) > 0) {
            throw new BusinessException("该类型有关联菜品,无法删除");
        }

        if (setmealMapper.findSetMealByCategoryId(id) > 0) {
            throw new BusinessException("该类型有关联套餐,无法删除");
        }
        return categoryMapper.delete(id) > 0;
    }

    /**
     * 添加
     *
     * @param category
     * @return
     */
    @Override
    public boolean save(Category category) {
        Category categoryByName = categoryMapper.getByName(category);
        if (categoryByName != null) {
            throw new BusinessException("添加的分类名已存在，请核实后在添加");
        }
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        return categoryMapper.save(category) > 0;
    }

    /**
     * 分页查询
     *
     * @param page     第几页
     * @param pageSize 页面容量
     * @return
     */
    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        //设置分页大小
        PageHelper.startPage(page, pageSize);

        List<Category> categoryList = categoryMapper.findAll();
        //创建分页
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        return new Page<>(
                pageInfo.getList(), pageInfo.getTotal(),
                pageInfo.getPageSize(), pageInfo.getPageNum()
        );
    }
}
